﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Facebook.Service.Core;
using FriendsNearby.Services;
using System.Collections.Generic;
using FriendsNearby.Services.Entities;
using FriendsNearby.Services.Facebook;
using Facebook.Service;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace FriendsNearby.Fbml
{
	public partial class RequestIntroduction : System.Web.UI.Page
	{
		FriendsNearbySystem friendsNearbySystem;
		string nameOfMutualFriendIfSingleAndNonHidden = string.Empty;
		IUsersService userService;
		ISocialNetworkService socialNetworkService;
		UserRegistry user;

		protected void Page_Load(object sender, EventArgs e)
		{

			friendsNearbySystem = new FriendsNearbySystem();
			userService = friendsNearbySystem.GetService<IUsersService>();
			socialNetworkService = friendsNearbySystem.GetService<ISocialNetworkService>();
			user = userService.QueryUserRegistry(x => x.FacebookUserId == fbApplication.UserID);

			if (!IsPostBack)
			{
				FriendOfFriend.Value = Request["target_friendoffriend"];
			}

			Guid contactOfContactID = new Guid(FriendOfFriend.Value);
			AnonymousMutualText.Visible = !CanShowMutualFriends(contactOfContactID);

			if (!IsPostBack)
			{
				List<Guid> mutualFriends = new List<Guid>(socialNetworkService.GetFriendsInCommon(user.UserID, contactOfContactID));

				if (mutualFriends.Count == 0)
				{
					RequestFormElements.Visible = false;
					RequestSucceded.Visible = false;
					RequestNoCommonFriends.Visible = true;

					LogEntry logEntry = new LogEntry();
					logEntry.Message = string.Format("Somehow no mutual friends can be found within the Request introduction dialog. {0} Requesting an introduction to {1}", fbApplication.UserID, contactOfContactID);
					logEntry.Categories.Add("Oddities");
					logEntry.EventId = LogEventIds.NoMutualFriensInRequestIntroductionDialog;
					Logger.Write(logEntry);
				}

				if (CanShowMutualFriends(contactOfContactID))
				{
					if (mutualFriends.Count == 1)
					{
						MutualFriendListContainer.Visible = false;
						UserRegistry onlyMutualContact = userService.GetUserPreferences(mutualFriends[0]);
						nameOfMutualFriendIfSingleAndNonHidden = onlyMutualContact.DisplayName;
					}
					else
					{
						MutualFriendListContainer.Visible = true;

						foreach (Guid mutualContactID in mutualFriends)
						{
							UserRegistry mutualContact = userService.GetUserPreferences(mutualContactID);
							string displayName = mutualContact.DisplayName;

							MutualFriendList.Items.Add(new ListItem(displayName, mutualContactID.ToString("B")));
						}
					}
				}
				else
				{
					MutualFriendListContainer.Visible = false;
				}
			}
			if (MutualFriendList.SelectedItem != null) // a selection was made in the dropdown. normalize to hidden field.
			{
				MutualFriend.Value = MutualFriendList.SelectedValue;
			}

		}

		protected void Submit_Click(object sender, EventArgs e)
		{
			Guid singleMutualContactID = Guid.Empty;
			Guid contactOfContactID = Guid.Empty;

			try
			{
				singleMutualContactID = new Guid(MutualFriend.Value);
			}
			catch
			{
			}
			try
			{
				contactOfContactID = new Guid(FriendOfFriend.Value);
			}
			catch
			{
			}

			if (singleMutualContactID != Guid.Empty)
			{
				socialNetworkService.SendIntrodutionNotification(FriendRequestMessage.Text, user.UserID, contactOfContactID, singleMutualContactID);
			}
			else
			{
				foreach (Guid mutualContactID in socialNetworkService.GetFriendsInCommon(user.UserID, contactOfContactID))
				{
					socialNetworkService.SendIntrodutionNotification(FriendRequestMessage.Text, user.UserID, contactOfContactID, mutualContactID);
				}
			}
			RequestFormElements.Visible = false;
			RequestSucceded.Visible = true;
		}

		private bool CanShowMutualFriends(Guid userID)
		{
			UserRegistry friendOfFriendPreference = userService.GetUserPreferences(userID);
			return (friendOfFriendPreference.VisibilityLevel == (int)VisibilityLevel.Everyone || friendOfFriendPreference.VisibilityLevel == (int)VisibilityLevel.ContactsAndTheirContacts);
		}

		protected string GetMutualFriendNameIfOnlyOneExistsAndCanShow()
		{
			if (!String.IsNullOrEmpty(nameOfMutualFriendIfSingleAndNonHidden))
			{
				return String.Format(" ({0})", nameOfMutualFriendIfSingleAndNonHidden);
			}
			return string.Empty;
		}

	}
}
